/* 

FORMBA - Automatic form fill utility
Copyright (c) 2010 Dorian Garson

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.


Installation and use:

 - Requires JQuery version >= 1.3.2.  
 
 - Include this file in your web page's <HEAD> section after JQuery-1.3.2.js

 - If you're happy with the default selections, you're done.
 
 - To modify default selections:
 
   [TEXT BOX]
   Default action: Fill with the name or ID of the box.
   Specified action: Add an attribute named "formba".  The value of "formba" is the text to use:
   
   <input type="text" name="txtBox" formba="fill with THIS text, not the name of the element">

   [LIST BOX]
   Default action: Select the first item in the list.
   Specified action: Add an attribute named "formba" on the option you want chosen. Value can be any text and is ignored.
   
   <select><option formba="true" value="1">1</option></select>
   
   [RADIO BUTTONS]
   Default action: Select the first radio button.
   Specified action: Add an attribute named "formba" on the button you want chosen.  Value can be any text and is ignored.

*/

// Fill every text box on the page with the name of the text box, 
// or with the value specified in the FORMBA attribute
function fillTextBox(tb) 
{
    var val = tb.formba;
    if (!tb.formba) {
        val = tb.id || tb.name;
        if (val.length > 12) {
            val = val.substring(val.length - 12, val.length);
        }
    }
    tb.value = val; 
}

// Select the first radio button in each group by default,
// or the radio button button that has the FORMBA attribute set
function pickRadioButton(rb)
{
    // If attribute formba="true" is on the radio element, select that one
    var rbFormba = $(rb).parent().find("input[type=radio][formba=true]")[0];
    
    if (rbFormba)
    {
        rbFormba.checked = true;
    }
    else
    // Otherwise just pick the first one
    {
        var rbSet = $(rb).parent().find("input[type=radio]");
        rbSet[0].checked = true;
    }
    $(rb).click();
}

// Select the SECOND list item in each list box, 
// or the one that contains the FORMBA attribute.
function pickListItem(lb) 
{
    if (lb.disabled) return;
    
    // Choose the SECOND one by default
    if (lb.length > 1)
        lb[1].selected = true;
    else
        lb[0].selected = true;
    
    for (var i = 0; i < lb.length; i++)
        if (lb[i].formba)
            lb[i].selected = true;
}

$(document).bind('keyup', function(e) {
    // Capture keystrokes and see if it's the magic FORMBA command (ctrl+shift+F)
    // If the user is invoking FORMBA, then call the selector functions for text boxes,
    // radio buttons, and lists.

    if (!e.shiftKey) return;
    if (!e.ctrlKey) return;

    var code = (e.keyCode ? e.keyCode : e.which);
    if (code != 70) return; // ctrl+shift+F for ie and ff, respectively

    // Set focus to first (non-hidden) input field
    var firstTb = $("input:visible:text:first");
    if (firstTb.length > 0) {
        firstTb.focus(); 
        firstTb[0].click();
    }
    
    // Set Formba values for Text Boxes
    $(":input[type=text]:enabled,textarea").each(function() { fillTextBox(this) });
    
    // Choose Radio Buttons
    $(":input[type=radio]").each(function() { pickRadioButton(this) });
    
    // Select List Items
    $("select").each(function() { pickListItem(this) });
});
